package com.graduation.alien_yuan.smartmusicplayer.widgets;

import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Gallery;

public class GalleryView extends Gallery {

    private OnItemSelectedListener onItemSelectedListener;
    private int preSeletedPosition;
    private View preSelectedView;

    public GalleryView(Context paramContext) {
        super(paramContext);
    }

    public GalleryView(Context paramContext, AttributeSet paramAttributeSet) {
        super(paramContext, paramAttributeSet);
    }


    @Override
    public void setOnItemSelectedListener(OnItemSelectedListener listener) {
        this.onItemSelectedListener = listener;
        super.setOnItemSelectedListener(new OnItemAnimationSelectedListener());
    }

    public class OnItemAnimationSelectedListener implements AdapterView.OnItemSelectedListener{
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {


            View s = parent.getChildAt(preSeletedPosition);
            s = preSelectedView;
            AnimatorSet animatorSet = new AnimatorSet();
            Log.d("alien", "     position = " + position + "   s=");
            if (s!= null){
                ObjectAnimator imgScaleDownYAnim = ObjectAnimator.ofFloat(s, "scaleY", 1f, 0.8f);
                imgScaleDownYAnim.setDuration(500);
                ObjectAnimator imgScaleDownXAnim = ObjectAnimator.ofFloat(s, "scaleX", 1f, 0.8f);
                imgScaleDownXAnim.setDuration(500);
                animatorSet.playTogether(imgScaleDownXAnim, imgScaleDownYAnim);
                animatorSet.start();

                Log.d("alien","     -----------  "+"preSelectedPosition = "+preSeletedPosition);
            }

            ObjectAnimator imgScaleUpYAnim = ObjectAnimator.ofFloat(view, "scaleY", 0.8f, 1f);
            imgScaleUpYAnim.setDuration(500);
            ObjectAnimator imgScaleUpXAnim = ObjectAnimator.ofFloat(view, "scaleX", 0.8f, 1f);
            imgScaleUpXAnim.setDuration(500);
            animatorSet.playTogether(imgScaleUpYAnim, imgScaleUpXAnim);
            animatorSet.start();



//            for (int i = 0; i < parent.getChildCount(); i++) {
//                if (parent.getChildAt(i) != view) {
//                    View s = parent.getChildAt(i);
//                    ObjectAnimator imgScaleDownYAnim = ObjectAnimator.ofFloat(s, "scaleY", 1f, 0.8f);
//                    imgScaleDownYAnim.setDuration(100);
//                    ObjectAnimator imgScaleDownXAnim = ObjectAnimator.ofFloat(s, "scaleX", 1f, 0.8f);
//                    imgScaleDownXAnim.setDuration(100);
//                    animatorSet.playTogether(imgScaleDownXAnim, imgScaleDownYAnim);
//                    animatorSet.start();
//                }
//            }
            preSeletedPosition = position;
            preSelectedView = view;
            onItemSelectedListener.onItemSelected(parent,view,position,id);
        }

        @Override
        public void onNothingSelected(AdapterView<?> parent) {
            onItemSelectedListener.onNothingSelected(parent);
            Log.d("alien","alien-onNothingSelected");
        }
    }

//        @Override
//    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
////        if (e1.getX() - e2.getX() < 0.0F) {
////            onKeyDown(KeyEvent.KEYCODE_DPAD_LEFT, null);
////        } else {
////            onKeyDown(KeyEvent.KEYCODE_DPAD_RIGHT, null);
////        }
//            Log.d("alien","onFling-  e1-"+e1.getAction()+"  e2-"+e2.getAction()+"  velocityX-"+velocityX+"   velocityY-"+velocityY);
//        return true;
//    }

}
